/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ysw.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import ysw.dao.ReceiveDao;
import ysw.databaseconnection.DBConn;
import ysw.vo.Receive;

/**
 *
 * @author Administrator
 */
public class ReceiveJdbc implements ReceiveDao {
    private  Connection conn=null;
    private PreparedStatement psmt=null;
    private ResultSet rs=null;
    public ReceiveJdbc(){}
    public Connection getConn(){
        try{
            if(this.conn==null||this.conn.isClosed()){
                DBConn mc=new DBConn();
                this.conn=mc.getConn();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return conn;
    }

    public Receive addReceive(Receive receive) {
      String sql="insert into receivess(eid,ename,ephone,carnumber,rtime,jieche) values(?,?,?,?,?,?)";
        try{
     
            psmt=this.getConn().prepareStatement(sql);
            
            psmt.setInt(1,receive.getEid());   
             psmt.setString(2,receive.getEname());  
       
             psmt.setString(3,receive.getEphone());
              psmt.setString(4,receive.getCarnumber());
                   psmt.setDate(5,receive.getRtime());
            
             psmt.setByte(6,receive.getJieche());
         
       
            psmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return receive;
    }

    
    public List showReceive() throws SQLException {
       String sql="select * from receivess";
       
        List receiveList=new ArrayList();
    try{
        psmt=this.getConn().prepareStatement(sql);
        rs=psmt.executeQuery();
        while(rs.next()){
            Receive receive=new Receive();
            receive.setId(rs.getInt("id"));
            receive.setEid(rs.getInt("eid"));
            receive.setEname(rs.getString("ename"));
           
            receive.setEphone(rs.getString("ephone"));
               receive.setCarnumber(rs.getString("carnumber"));
             receive.setRtime(rs.getDate("rtime"));
         
            receive.setJieche(rs.getByte("jieche")); 
            
            receiveList.add(receive);
     
  
        }
        return receiveList;
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
             if(rs!=null){
                   rs.close();
                   rs=null;
                }
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return receiveList;
    }
public Receive showOneReceive(Integer id){
     ResultSet rs=null;
    String sql="select * from receivess where id="+id;
   Receive receive=new Receive();
    try{
      psmt=this.getConn().prepareStatement(sql);
        rs=psmt.executeQuery();
        while(rs.next()){
            receive.setId(rs.getInt("id"));
            receive.setEid(rs.getInt("eid"));
            receive.setEname(rs.getString("ename"));
           
            receive.setEphone(rs.getString("ephone"));
             receive.setCarnumber(rs.getString("carnumber"));
             receive.setRtime(rs.getDate("rtime"));
           
            receive.setJieche(rs.getByte("jieche")); 
        }
 
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
            psmt.close();
              
            }catch(SQLException e){
                        e.printStackTrace();
                        }
        try{conn.close();
        
        }catch(SQLException e){
             e.printStackTrace();
        }
            }
            return receive;
}


 public Receive updateSaveReceive(Receive receive){
          String sql="update receivess set id=?,eid=?,ename=?,ephone=?,carnumber=?,rtime=?,jieche=? where id="+receive.getId();
     try{
            
          psmt=this.getConn().prepareStatement(sql);
          psmt.setInt(1,receive.getId());
             psmt.setInt(2,receive.getEid());   
             psmt.setString(3,receive.getEname());  
       
             psmt.setString(4,receive.getEphone());
             psmt.setString(5,receive.getCarnumber());
                   psmt.setDate(6,receive.getRtime());
             
             psmt.setByte(7,receive.getJieche());
          
            psmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
         try{
             psmt.close();
            
         }catch(SQLException e){
             e.printStackTrace();
         }
     }
     return receive;
} 
}
